package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.adapter.ExamineAllocaDetailBean
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.mine.PayPasswordActivity
import kotlinx.android.synthetic.main.activity_apply_detail_allow.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap


class ApplyDetailAllowActivity : BaseActivity(), IView {

    var type: String = "1" // 1 同意 2 拒绝
    override fun attachLayoutRes(): Int {
        return R.layout.activity_apply_detail_allow
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    override fun initData() {
        val params = HashMap<String, String>()
        params["id"] = intent.getStringExtra("id")
        params["key"] = token
        params["client"] = "android"

        RetrofitHelper.service.examineAllocationDetail(params).sss(this)
        {
            setViewData(it.result)
        }
    }

    override fun initView() {
        tv_title.text = "申请明细"
        iv_back.setOnClickListener { finish() }
        btn_take_agree.setOnClickListener {ConfirmMoneyOne("1","同意伙伴申请")  }
        btn_take_reject.setOnClickListener {ConfirmMoneyOne("2","拒绝伙伴申请")  }

    }

    override fun start() {

    }

    private fun setViewData(data: ExamineAllocaDetailBean) {
        tv_page_title.text = data.title
        tv_name.text = data.member_name
        tv_phone.text = data.member_mobile
        tv_company.text = data.gc_name
        tv_machine_name.text = data.goods_name
        tv_machine_model.text = data.goods_serial

        val split = data.decoration!!.split("|")
        val stringBuilder = StringBuilder()
        for (i in 0 until split.size - 1) {
            if (i == split.size - 2) {
                stringBuilder.append(split[i])
            } else {
                stringBuilder.append(split[i]).append("\n")
            }

        }

        tv_content.text = " $stringBuilder"
        when (data.examine_type) {
            "0" -> {
                tv_status.text = "待上级审核"
                tv_take_reject.visibility = View.GONE
                tv_fail_reason.visibility = View.GONE
                tv_reason.visibility = View.GONE
                if (data.is_type == 1) {
                    ll_check.visibility = View.VISIBLE
                } else {
                    ll_check.visibility = View.GONE
                }
            }
            "1" -> {
                tv_status.text = "待平台审核"
                tv_take_reject.text = "已同意"
                ll_check.visibility = View.GONE
                tv_fail_reason.visibility = View.GONE
                tv_reason.visibility = View.GONE
                if (data.is_type == 1) {
                    tv_take_reject.visibility = View.VISIBLE
                } else {
                    tv_take_reject.visibility = View.GONE
                }
            }
            "2" -> {
                tv_status.text = "审核通过"
                tv_take_reject.text = "已同意"
                tv_fail_reason.visibility = View.GONE
                tv_reason.visibility = View.GONE
                ll_check.visibility = View.GONE
                if (data.is_type == 1) {
                    tv_take_reject.visibility = View.VISIBLE
                } else {
                    tv_take_reject.visibility = View.GONE
                }
            }
            "3" -> {
                tv_status.text = "审核失败"
                tv_take_reject.text = "已拒绝"
                tv_reason.visibility = View.VISIBLE
                ll_check.visibility = View.GONE
                if (!TextUtils.isEmpty(data.audit_describe)) {
                    tv_reason.text = data.audit_describe
                    tv_reason.setTextColor(Color.parseColor("#ffff4444"))
                }
                tv_status.setTextColor(Color.parseColor("#ffff4444"))
                if (data.is_type == 1) {
                    tv_take_reject.visibility = View.VISIBLE
                } else {
                    tv_take_reject.visibility = View.GONE
                }
            }
            "4" -> {
                tv_status.text = "待下级审核"
                tv_take_reject.visibility = View.GONE
                tv_fail_reason.visibility = View.GONE
                tv_reason.visibility = View.GONE
                if (data.is_type == 1) {
                    ll_check.visibility = View.VISIBLE
                } else {
                    ll_check.visibility = View.GONE
                }
            }
            "5" -> {
                tv_status.text = "待审核"
                tv_take_reject.visibility = View.GONE
                tv_fail_reason.visibility = View.GONE
                tv_reason.visibility = View.GONE
                if (data.is_type == 1) {
                    ll_check.visibility = View.VISIBLE
                } else {
                    ll_check.visibility = View.GONE
                }
            }
            else -> tv_status.text = "未知状态"
        }
    }


    private fun ConfirmMoneyOne(type: String, message: String) {
        this.type = type
        Intent(this, PayPasswordActivity::class.java).run {
            putExtra("activity_title", "验证支付密码")
            putExtra("title", message)
            putExtra("check_message", " ")
            putExtra("imp_type", 2) // 验证支付密码
            startActivityForResult(this, 100)
        }
    }

    private fun ConfirmMoneyTwo() {
        val params = HashMap<String, String>()
        params["id"] = intent.getStringExtra("id")
        params["type"] = type
        params["key"] = token
        RetrofitHelper.service.parentExamine(params).sss(this) {
            showToast(it.result.toString())
            initData()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            ConfirmMoneyTwo()
        }
    }
}
